package com.hz.filemanager;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adjust.sdk.Adjust;
import com.hz.filemanager.ad.utils.RemoteConfig;
import com.hz.filemanager.model.FileDataManager;
import com.hz.filemanager.model.clean.ThridApp;
import com.hz.filemanager.model.file.ImgFolderBean;
import com.hz.filemanager.utils.SPUtils;
import com.hz.filemanager.utils.Utils;
import com.hz.sdka.AdSDKa;

import java.util.List;

public class App extends Application {
    public static SPUtils spUtils;
    //省电扫描数据临时传递用
    public static List<ThridApp> thridAppList;

    public static List<ImgFolderBean> imgFolderBeanList;
    public static boolean animationADShow = false;
    private static int tabNum = 0;

    @Override
    public void onCreate() {
        super.onCreate();

        Utils.init(this);
        spUtils = new SPUtils("clean_sp");
        FileDataManager.getInstance();

        if (getPackageName().equals(getCurrentProcessName())) {
//            // 初始化设备风险SDK，init接口需要在APP启动尽可能早的时候调用。
//            parserChannel(this);
            initATSDK();
            RemoteConfig.INSTANCE.init();
            registerActivityLifecycleCallbacks(new AdjustLifecycleCallbacks());//必須实现adjust的onResume和onPause方法
        }
    }


    /**
     * 初始化广告
     */
    private void initATSDK() {

        AdSDKa.getInstance().setDebug(BuildConfig.DEBUG);//启动广告日志 可通过过滤think 查看日志
        AdSDKa.getInstance().init(this, BuildConfig.TOP_ON_APP_ID, BuildConfig.TOP_ON_APP_KEY, BuildConfig.AD_DEV_KEY);
//        ATSDK.testModeDeviceInfo(getContext(), s -> Log.i("wade", s));
    }

    public static int getTabNum() {
        return tabNum;
    }

    public static void addTabNum() {
        App.tabNum++;
    }

    private final class AdjustLifecycleCallbacks implements ActivityLifecycleCallbacks {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            Adjust.onResume();
        }

        @Override
        public void onActivityPaused(Activity activity) {
            Adjust.onPause();
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    }

    /**
     * 获取当前进程名
     */
    private String getCurrentProcessName() {
        int pid = android.os.Process.myPid();
        String processName = "";
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
            if (process.pid == pid) {
                processName = process.processName;
            }
        }
        return processName;
    }
}
